<html>
<head>
<meta charset="UTF-8">
<title>用户角色设置</title>
<link id="easyuiTheme"  href="../../static/easyui/themes/bootstrap/easyui.css" rel="stylesheet" type="text/css"/>
<link href="../../static/easyui/themes/icon.css" rel="stylesheet" type="text/css"/>
    <link href="../../static/css/esaas-easyui-add.css" rel="stylesheet" type="text/css"/>
    <script src="../../static/easyui/jquery.min.js" type="text/javascript"></script>
    <script src="../../static/easyui/jquery.cookie.js" type="text/javascript"></script>
    <script src="../../static/easyui/ui-theme.js" type="text/javascript"></script>
<script src="../../static/easyui/jquery.easyui.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../static/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
<script src="../../static/plugin/assets/jslib.util.js" type="text/javascript"></script>
<script src="../../static/plugin/assets/jslib.jquery.ajax.js" type="text/javascript"></script>
<script src="../../static/plugin/assets/jslib.easyui.util.js" type="text/javascript"></script>
<script src="../../ffres/dict/all.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="../../static/bootstrap/css/bootstrap.css">
<script type="text/javascript" src="../../static/bootstrap/js/bootstrap.js"></script>
<a href="" target="_blank" id="commonLink" name="commonLink"></a>
</head>
<body>
<script type="text/javascript">
    //全局变量用户编号
    var actorNo;

    $(function(){
        addTitle("设置角色")
        actorNo = $.getUrlParam('actorno');
        getRolesList();
        getRoleUserList();
        var j_all_area = $("#sel_all_area"), j_selected_areas = $("#sel_selected_areas");
        $("#selectAll").click(function(){
            $.messager.confirm('全选提示', '是否确定全选？确认后将立即生效。', function(r){
                if (r){
                    var selectedAreaArray = [];
                    j_all_area.find("option").each(function(i){
                        selectedAreaArray[i] = $(this).val();
                        $(this).appendTo(j_selected_areas);
                    });
                    doSubmit('add',selectedAreaArray.toString());
                }
            });
            return false;
        });
        $("#selectAct").click(function(){
            var selectedAreaArray = [];
            j_all_area.find("option:selected").each(function(i){
                selectedAreaArray[i] = $(this).val();
                $(this).appendTo(j_selected_areas);
            });
            if(null == selectedAreaArray || 0== selectedAreaArray.length){
                return false;
            }
            doSubmit('add',selectedAreaArray.toString());
            return false;
        });
        $("#removeAct").click(function(){
            var selectedAreaArray = [];
            j_selected_areas.find("option:selected").each(function(i){
                selectedAreaArray[i] = $(this).val();
                $(this).appendTo(j_all_area);
            });
            if(null == selectedAreaArray || 0== selectedAreaArray.length){
                return false;
            }
            doSubmit('del',selectedAreaArray.toString());
            return false;
        });
        $("#removeAll").click(function(){
            $.messager.confirm('删除提示', '是否确定全部删除？确认后将立即生效。', function(r){
                if (r){
                    var selectedAreaArray = [];
                    j_selected_areas.find("option").each(function(i){
                        selectedAreaArray[i] = $(this).val();
                        $(this).appendTo(j_all_area);
                    });
                    doSubmit('del',selectedAreaArray.toString());
                }
            });
            return false;
        })
    });

    //获取可选用户列表
    function getRolesList(){
        $.ajax({
            type: "POST",
            url: '../../api/s/roles/not/used',
            data: {
                actorno: actorNo
            },
            success: function(data) {
                creatRolesHtml(data.rows);
            }
        });
    }
    //创建可选用户HTML
    function creatRolesHtml(roles) {
        var rolesHtml = "";
        if (roles != null && roles.length > 0) {
            for (var i = 0; i < roles.length; i++) {
                rolesHtml += ("<option value='" + roles[i].roleno + "'>" + roles[i].rolename);
                rolesHtml += "</option>";
            }
        }
        document.getElementById("sel_all_area").innerHTML = rolesHtml.toString();
    }

    //获取用户已设置角色列表
    function getRoleUserList(){
        $.ajax({
            type: "POST",
            url: '../../api/s/role/user/all',
            data: {
                actorno: actorNo
            },
            success: function(data) {
                creatRolesUserHtml(data.rows);
            }
        });
    }
    //创建用户已设置角色HTML
    function creatRolesUserHtml(roleUserList) {
        var rolesHtml = "";
        if (roleUserList != null && roleUserList.length > 0) {
            for (var i = 0; i < roleUserList.length; i++) {
                rolesHtml += ("<option value='" + roleUserList[i].roleno + "'>" + roleUserList[i].rolename);
                rolesHtml += "</option>";
            }
        }
        document.getElementById("sel_selected_areas").innerHTML = rolesHtml.toString();
    }

    //用户角色设置操作保存
    function doSubmit(select_type, roleids){
        var jsonData = JSON.parse('{'+'"actorno":"' + actorNo + '"' + ','+'"roleNostr":"' + roleids + '"' +'}');
        if('add' === select_type){
            $.restfulPostJson('../../api/s/roles/user', jsonData, function(data) {
                if(data.rows > 0){
                    $.messager.alert('提示', '保存成功！','info');
                }
            });
        }else if('del' === select_type){
            $.restfulDelete('../../api/s/roles/user', jsonData, function(data) {
                if(data.rows > 0){
                    $.messager.alert('提示', '删除成功！','info');
                }
            });
        }else{
        }
    }

    function closeForm() {
        window.parent.JLEUtil.closewin();
    }

</script>

    <fieldset>
        <table class="table table-bordered dchannel-table">
            <tbody>
            <tr class="item-default">
                <thead>
                <tr>
                    <th>可选角色</th>
                    <th></th>
                    <th>已选角色</th>
                </tr>
                </thead>
                <td align="right" style="width: 50%;">
                    <select id="sel_all_area" multiple="multiple" size="20" style="width:100%;">

                    </select>
                </td>
                <td style="width: 50px; padding-top:100px;">
                    <button type="button" class="btn btn-default btn-small" id="selectAll"><span class="glyphicon glyphicon-forward"></span></button>
                    <button type="button" class="btn btn-default btn-small" id="selectAct"><span class="glyphicon glyphicon-chevron-right"></span></button>
                    <button type="button" class="btn btn-default btn-small" id="removeAct"><span class="glyphicon glyphicon-chevron-left"></span></button>
                    <button type="button" class="btn btn-default btn-small" id="removeAll"><span class="glyphicon glyphicon-backward"></span></button>
                </td>
                <td style="width: 50%;">
                    <select id="sel_selected_areas"  multiple="multiple" size="20" style="width:100%;">

                    </select>
                </td>
            </tr>
            </tbody>
        </table>
    </fieldset>

<div class="esaas-info-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="closeForm()" style="width:90px">&nbsp;关&nbsp;闭&nbsp;</a>
</div>
</body>
</html>

